package org.dftproject.genesis.ui.agents.pages;

import org.dftproject.genesis.core.agents.AgentManager;
import org.dftproject.genesis.core.agents.IAgent;
import org.dftproject.genesis.core.agents.IAgentManagerListener;
import org.dftproject.genesis.ui.Activator;
import org.dftproject.genesis.ui.agents.figures.AgentFigure;
import org.dftproject.genesis.ui.figures.FigureCanvasEx;
import org.dftproject.genesis.ui.figures.HyperlinkFigure;
import org.dftproject.genesis.ui.figures.IHyperlinkListener;
import org.dftproject.genesis.ui.figures.PageContainer;
import org.dftproject.genesis.ui.figures.UniformSizeGroup;
import org.dftproject.genesis.ui.pages.AbstractPagePart;
import org.dftproject.genesis.ui.pages.IPageInput;
import org.dftproject.genesis.ui.pages.IPageSite;
import org.dftproject.genesis.ui.pages.PageInitException;
import org.dftproject.genesis.ui.pages.PageManager;
import org.dftproject.genesis.ui.pages.PageUtils;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class AgentsPage extends AbstractPagePart {

	public static final String ID = AgentsPage.class.getName();

	private IAgentManagerListener agentManagerListener;

	private FigureCanvas canvas;
	private Figure agentList;

	public void init(IPageSite site, IPageInput input) throws PageInitException {
		this.setSite(site);
		this.setInput(input);
	}

	@Override
	public void createPartControl(Composite parent) {
		agentManagerListener = new IAgentManagerListener() {

			public void agentAdded(IAgent agent) {
				updateAgents();
			}

			public void agentRemoved(IAgent agent) {
				updateAgents();
			}

			public void agentUpdated(IAgent agent) {
				updateAgents();
			}
			
		};
		AgentManager.getDefault().addListener(agentManagerListener);
		
		createContents(parent);
	}

	protected void createContents(Composite parent) {
		// Create the main Draw2D canvas

		canvas = new FigureCanvasEx(parent);
		canvas.getViewport().setContentsTracksWidth(true);

		// Create the root figure

		Figure container = new PageContainer();
		canvas.setContents(container);

		container.add(PageUtils.createH1Label("Automatic Research and Analysis"));

		container.add(PageUtils.createFlowedText("Agents help you with your family history by automatically performing research and analysis."));

		IFigure linkContainer = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(false);
		linkContainer.setLayoutManager(layout);
		linkContainer.setBorder(new MarginBorder(10, 10, 0, 0));
		container.add(linkContainer);

		HyperlinkFigure addLink = new HyperlinkFigure("Start an Agent", Activator.getImageDescriptor("icons/add.png").createImage());
		addLink.addHyperlinkListener(new IHyperlinkListener() {

			public void linkActivated(Object data, int stateMask) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
				IWorkbenchPage workbenchPage = window.getActivePage();
				PageManager.getDefault().openPage(workbenchPage, new NewAgentPageInput(), NewAgentPage.ID, true, (stateMask & SWT.CTRL) != 0);
			}

		});
		linkContainer.add(addLink);
		
		agentList = new Figure();
		layout = new ToolbarLayout(false);
		layout.setStretchMinorAxis(false);
		agentList.setLayoutManager(layout);
		agentList.setBorder(new MarginBorder(10, 0, 0, 0));
		container.add(agentList);
		
		UniformSizeGroup linkGroup = new UniformSizeGroup();
		for (IAgent agent : AgentManager.getDefault().getAgents()) {
			AgentFigure figure = new AgentFigure(agent, linkGroup);
			agentList.add(figure);
		}
	}

	protected void updateAgents() {
		Display.getDefault().asyncExec(new Runnable() {

			public void run() {
				agentList.removeAll();
				
				UniformSizeGroup linkGroup = new UniformSizeGroup();
				for (IAgent agent : AgentManager.getDefault().getAgents()) {
					AgentFigure figure = new AgentFigure(agent, linkGroup);
					agentList.add(figure);
				}
			}
			
		});
	}

	@Override
	public void dispose() {
	}

	@Override
	public void setFocus() {
		canvas.setFocus();
	}

}
